<?php

class SystemAccountPermisionController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	//public $layout='//layouts/column2';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return $this->getRules();
	}

	

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
            $pagevals = array(
                'page_title' => "Phân quyền",
                'subpage_title' => 'Phân quyền tài khoản hệ thống',
            );
            
            $pagevals['module'] = isset($_GET['module']) ? $_GET['module'] : 'game';
            $pagevals['system_account'] = isset($_GET['system_account']) ? $_GET['system_account'] : Yii::app()->user->name;
            
            if(isset($_POST['system_account'])){
                 $pagevals['module'] = $_POST['module'];
                 $pagevals['system_account'] = $_POST['system_account'];
            }
           
            //get system_account name
            
            $permisions = SystemAccountPermisionModel::model()->findByAttributes(array('module'=> $pagevals['module'],'username'=>$pagevals['system_account']));
            if(is_object($permisions)){
                $pagevals['permision'] = explode(',', $permisions->permisions);
            }else{
                $pagevals['permision']=array();
            }
            

            $system_account_filter_laber = array();
            $system_accounts = SystemAccountModel::model()->getAll();
            if(is_array($system_accounts)){
                foreach($system_accounts as $value){
                    if($value != null){ 
                       $system_account_filter_laber[$value['username']]  = $value['username'];
                    }
                }
            }
            $pagevals['selection_label']['system_account'] = $system_account_filter_laber;
            $pagevals['selection_label']['module']= array(
                                                        'site'=>'Site',
                                                        'post'=>'Post',
                                                        'game'=>'Game',
                                                        'pcategory'=>'Post Category',
                                                        'gcategory'=>'Game Category',
                                                        'provider'=>'Provider',
                                                        'developer'=>'Developer',
                                                        'user'=>'User',
                                                        'systemaccount'=>'System Account',
                                                        'systemaccountpermision'=>'System Account Permision',
                                                        'config'=>'Config',
                                                    );
            
            
           if(isset($_POST['system_account'])){
                $permisions_array= array();
                $permisions_string =  null;
                $permisions_array['index'] = isset($_POST['index']) ? $_POST['index'] : null;
                $permisions_array['view'] = isset($_POST['view']) ? $_POST['view'] : null;
                $permisions_array['active'] = isset($_POST['active']) ? $_POST['active'] : null;
                $permisions_array['pending'] = isset($_POST['pending']) ? $_POST['pending'] : null;
                $permisions_array['block'] = isset($_POST['block']) ? $_POST['block'] : null;
                $permisions_array['lock'] = isset($_POST['lock']) ? $_POST['lock'] : null;
                $permisions_array['waiting'] = isset($_POST['waiting']) ? $_POST['waiting'] : null;
                $permisions_array['banned'] = isset($_POST['banned']) ? $_POST['banned'] : null;
                $permisions_array['restore'] = isset($_POST['restore']) ? $_POST['restore'] : null;
                $permisions_array['remove'] = isset($_POST['remove']) ? $_POST['remove'] : null;
                $permisions_array['edit'] = isset($_POST['edit']) ? $_POST['edit'] : null;
                $permisions_array['create'] = isset($_POST['create']) ? $_POST['create'] : null;
                $permisions_array['approve'] = isset($_POST['approve']) ? $_POST['approve'] : null;
                foreach($permisions_array as $value){
                    if($value != null){
                        $permisions_string .= $value.',';
                    }
                }
                $permisions_string = substr($permisions_string, '0','-1');
                
                if(!is_object($permisions)){
                    $permisions = new SystemAccountPermisionModel;
                    $permisions->attributes = array(
                                                    'username'=>$_POST['system_account'],
                                                    'module'=>$_POST['module'],
                                                    'permisions'=>$permisions_string,
                                                );
                    if($permisions->save(false)){
                        $this->redirect(array('index','module'=>$pagevals['module'],'system_account'=>$pagevals['system_account']));
                    }
                }
                else {
                    $permisions->attributes = array(
                                                    'username'=>$_POST['system_account'],
                                                    'module'=>$_POST['module'],
                                                    'permisions'=>$permisions_string,
                                                );
                    if($permisions->save(false)){
                        $this->redirect(array('index','module'=>$pagevals['module'],'system_account'=>$pagevals['system_account']));
                    }
                }
                
            }
            
            $this->display('systemAccountPermision/index', array('pagevals'=>$pagevals));
	}

	
}
